/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uta.tsrh.db.commandpattern;

import com.mysql.jdbc.Connection;
import edu.uta.tsrh.db.DAO;
import edu.uta.tsrh.db.DBQueryExecuter;
import edu.uta.tsrh.model.HashMD5;

/**
 *
 * @author Bugs
 */
public class CreateUser implements MySQLCmd {

    private String userName;
    private String password;
    private String emailId;
    
    public CreateUser(String userName, String password, String emailId) {
        this.userName = userName;
        this.password = password;
        this.emailId = emailId;
    }

    @Override
    public Object execute() {
        
        //Hash the apssword
        this.password = HashMD5.hashpw(this.password);
        
        String sqlQuery = buildQuery();
        System.out.println("query is " + sqlQuery);      

        Connection connection = DAO.getConnection();

        DBQueryExecuter queryExecuter = new DBQueryExecuter();
        int success = queryExecuter.modify(sqlQuery, connection);

        DAO.closeConnection();

        if (success == 1) {
            return true;
        } else {
            return false;
        }
    }

    private String buildQuery() {

        return "Insert into user(userName,password,emailId) values('" + this.userName + "','" + this.password + "','" + this.emailId + "')";
    }

    @Override
    public boolean isReversible() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void undo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
